package com.redwine.app.utils;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.hyphenate.EMCallBack;
import com.redwine.app.APP;
import com.redwine.app.chat.ChatHelper;
import com.redwine.app.net.model.User;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by tian on 15/4/17:下午5:12.
 */
public class Settings {

    public static final String PREF_NAME = "preferences";

    public static final List<String> readArticles = new ArrayList<>();

    static Context gContext;

    @SuppressLint("InlinedApi")
    public static SharedPreferences getPrefs() {
        if (gContext == null) {
            gContext = APP.getInstance();
        }
        int code = Context.MODE_PRIVATE;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            code = Context.MODE_MULTI_PROCESS;
        }
        code |= Context.MODE_APPEND;
        return gContext.getSharedPreferences(PREF_NAME, code);
    }

    private static final String FIRST_LAUNCH_KEY = "first_launch";

    public static boolean isFirstLaunch() {
        SharedPreferences pref = getPrefs();
        boolean isFirst = pref.getBoolean(FIRST_LAUNCH_KEY, true);
        if (isFirst) {
            SharedPreferences.Editor editor = pref.edit();
            editor.putBoolean(FIRST_LAUNCH_KEY, false);
            editor.apply();
        }
        return isFirst;
    }

    //qcl第三方qq相关
    private static final String QQ_KEY = "qq";
    private static final String QQ_UID_KEY = "qq_uid";
    private static final String QQ_TOKEN = "qq_token";
    private static final String QQ_NICKNAME = "qq_nickname";

    public static void saveQQUid(String id) {
        SharedPreferences pref = getPrefs();
        SharedPreferences.Editor editor = pref.edit();
        if (!TextUtils.isEmpty(id)) {
            editor.putString(QQ_UID_KEY, "");
        }
        editor.putString(QQ_UID_KEY, id);
        editor.apply();
    }

    public static String getQQUid() {
        return getPrefs().getString(QQ_UID_KEY, "");
    }

    public static void saveQQToken(String QQToken) {
        SharedPreferences pref = getPrefs();
        SharedPreferences.Editor editor = pref.edit();
        if (!TextUtils.isEmpty(QQToken)) {
            editor.putString(QQ_TOKEN, "");
        }
        editor.putString(QQ_TOKEN, QQToken);
        editor.apply();
    }

    public static String getQQNickname() {
        return getPrefs().getString(QQ_NICKNAME, "");
    }

    public static void saveQQNickname(String QQNickname) {
        SharedPreferences pref = getPrefs();
        SharedPreferences.Editor editor = pref.edit();
        if (!TextUtils.isEmpty(QQNickname)) {
            editor.putString(QQ_NICKNAME, "");
        }
        editor.putString(QQ_NICKNAME, QQNickname);
        editor.apply();
    }

    public static String getQQToken() {
        SharedPreferences pref = getPrefs();
        String QQToken = pref.getString(QQ_TOKEN, "");
        return QQToken;
    }
    //    public static void saveQQAccessToken(Oauth2AccessToken accessToken) {
    //        SharedPreferences pref = getPrefs();
    //        SharedPreferences.Editor editor = pref.edit();
    //        editor.putString(QQ_TOKEN, accessToken.getToken());
    //        editor.putLong(QQ_EXPIRE_TIME, accessToken.getExpiresTime());
    //        editor.apply();
    //    }
    //
    //    public static Oauth2AccessToken readQQAccessToken() {
    //        Oauth2AccessToken token = new Oauth2AccessToken();
    //        SharedPreferences pref = getPrefs();
    //        token.setToken(pref.getString(QQ_TOKEN, ""));
    //        token.setExpiresTime(pref.getLong(QQ_EXPIRE_TIME, 0));
    //        return token;
    //    }
    //qcl第三方qq相关


    //    private static final String QQ_EXPIRE_TIME = "qq_expire_time";
    private static final String QQ_DISPLAY_NAME = "qq_display_name";
    private static final String TOKEN_KEY = "token";
    private static final String UID_KEY = "uid";


    //sina相关qcl
    private static final String SINA_WEIBO_TOKEN = "sina_weibo_token";
    private static final String SINA_WEIBO_EXPIRE_TIME = "sina_weibo_expire_time";
    private static final String WEIBO_UID_KEY = "weibo_uid";
    private static final String WEIBO_DISPLAY_NAME = "weibo_display_name";


    public static void saveWeiboUid(String id) {
        SharedPreferences pref = getPrefs();
        SharedPreferences.Editor editor = pref.edit();
        editor.putString(WEIBO_UID_KEY, id);
        editor.apply();
    }

    public static String getWeiboUid() {
        return getPrefs().getString(
                WEIBO_UID_KEY, "");
    }

    //判断是否登录过
    public static final String PROFILE = "profile";


    public static void saveProfile(String profile) {
        getPrefs().edit().putString(PROFILE, profile).apply();
    }

    public static void saveProfile(User user) {
        if (null != user) {
            Gson gson = new Gson();
            String json = gson.toJson(user);
            saveProfile(json);
        }
    }


    public static String getProfile() {
        return getPrefs().getString(PROFILE, "");
    }


    //判断是否登录过
    public static final String ABOUTUS = "aboutus";


    public static void saveAboutUs(String aboutus) {
        getPrefs().edit().putString(ABOUTUS, aboutus).apply();
    }

    public static String getAboutUs() {
        return getPrefs().getString(ABOUTUS, "");
    }


    //判断是否登录过
    public static boolean isLoggedIn() {
        String userInfo = getProfile();
        if (!TextUtils.isEmpty(userInfo) && null != Global.user) {
            return true;
        }
        return false;
    }

    public static String getToken() {
        return getPrefs().getString(TOKEN_KEY, "");
    }


    /**
     * 清空账号信息
     * 作者：gassion on 17/8/26 18:21
     * 最可怕的敌人，就是没有坚强的信念。
     */
    public static void clearLogInInfo() {
        SharedPreferences preferences = getPrefs();
        SharedPreferences.Editor editor = preferences.edit();
        editor.remove(PROFILE);
        editor.apply();
    }


    public static void logOut() {
        logOutHuanxin();
        clearLogInInfo();
        getPrefs().edit().clear().apply();
    }

    /**
     * 退出环信
     */
    private static void logOutHuanxin() {
        ChatHelper.getInstance().logout(false, new EMCallBack() {

            @Override
            public void onSuccess() {
            }

            @Override
            public void onProgress(int progress, String status) {

            }

            @Override
            public void onError(int code, String message) {

            }
        });
    }


    //供应商的状态
    public static final String SUPPLIER_STATUS = "supplier_status";
    //采购商的状态
    public static final String BUYER_STATUS = "buyer_status";

    /**
     * 存入数据
     *
     * @param key
     * @param value
     */
    public static void setSharedPreferences(String key, String value) {
        getPrefs().edit().putString(key, value).apply();
    }

    /**
     * 读取数据
     *
     * @param key
     * @return
     */
    public static String getSharedPreferences(String key) {
        return getPrefs().getString(key, "");
    }

}
